package com.masz.seatathree.service;

import com.alibaba.fastjson.JSONObject;
import io.seata.rm.tcc.api.BusinessActionContext;
import io.seata.rm.tcc.api.LocalTCC;
import io.seata.rm.tcc.api.TwoPhaseBusinessAction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.client.RestTemplate;

/**
 * @author masz
 * @Date 2022/2/14 15:21
 * seata tcc
 */
@Service
public class SeataThreeTccService implements SeataThreeTccServiceIntf {

    @Autowired
    RestTemplate restTemplate;

    @Transactional(rollbackFor = Exception.class)
    public void tcc(BusinessActionContext businessActionContext){
        System.out.println("tcc three ");
    }

    @Transactional(rollbackFor = Exception.class)
    public boolean commit(BusinessActionContext businessActionContext){
        System.out.println("commit tcc three ");
        return true;
    }

    @Transactional(rollbackFor = Exception.class)
    public boolean rollback(BusinessActionContext businessActionContext){
        System.out.println("rollback tcc three ");
        return true;
    }


    public void rpcTccTwo(String v){
        JSONObject date = new JSONObject();
        date.put("v", v + "-two");
        restTemplate.postForEntity("http://seata-two/seata/tcc", date,String.class);
    }

    public void rpcTccThree(String v){
        JSONObject date = new JSONObject();
        date.put("v", v + "-three");
        restTemplate.postForEntity("http://seata-three/seata/tcc", date,String.class);
    }

}
